/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.login.plugin.h;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import com.qihoo.gamecenter.sdk.common.i.g;
import com.qihoo.gamecenter.sdk.common.i.k;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class l {
    public static void a(Context paramContext, String paramString, Handler paramHandler) {
        a(paramContext, paramString, paramHandler, 0);
    }

    public static void a(Context paramContext, String paramString, Handler paramHandler, int paramInt) {
        new Thread(new Runnable(paramContext, paramString, paramHandler, paramInt) {
            public void run() {
                Bitmap localBitmap = l.a(this.a, this.b);
                this.c.obtainMessage(this.d, localBitmap).sendToTarget();
            }
        }).start();
    }

    public static Bitmap a(Context paramContext, String paramString) {
        File localFile1 = new File(paramContext.getCacheDir(), "remote_pic");
        if (!(localFile1.exists()))
            localFile1.mkdirs();
        a(localFile1);
        String str1 = k.a(paramString);
        File localFile2 = new File(localFile1, str1);
        String str2 = localFile2.getAbsolutePath();
        Bitmap localBitmap = null;
        if (localFile2.exists()) {
            localBitmap = BitmapFactory.decodeFile(str2);
            e.a("Utils.RemoteBitmap", new Object[] { "loadPic from cache : " + str2 });
        }
        if (null == localBitmap) {
            e.a("Utils.RemoteBitmap", new Object[] { "loadPic from url begin : " + paramString });
            String str3 = localFile2.getAbsolutePath() + "_temp";
            File localFile3 = new File(str3);
            try {
                URL localURL = new URL(paramString);
                URLConnection localURLConnection = localURL.openConnection();
                localURLConnection.connect();
                InputStream localInputStream = localURLConnection.getInputStream();
                g.a(localInputStream, localFile3);
            } catch (Exception localException1) {
                e.b("Utils.RemoteBitmap", "loadPic from url error : " + localException1.getMessage(), localException1);
            }
            if (localFile3.exists()) {
                if (localFile2.exists()) {
                    e.a("Utils.RemoteBitmap", new Object[] { "delete org cache file" });
                    localFile2.deleteOnExit();
                }
                try {
                    e.a("Utils.RemoteBitmap", new Object[] { "copy tempFile to cacheFile" });
                    g.a(localFile3, localFile2);
                } catch (Exception localException2) {
                    e.b("Utils.RemoteBitmap", "", localException2);
                    localFile3.renameTo(localFile2);
                }
                localFile3.deleteOnExit();
                localBitmap = BitmapFactory.decodeFile(str2);
            }
        }
        return localBitmap;
    }

    private static void a(File paramFile) {
        File[] arrayOfFile = paramFile.listFiles();
        long l = System.currentTimeMillis();
        File localFile = null;
        for (int i = 0; i < arrayOfFile.length; ++i) {
            localFile = arrayOfFile[i];
            if ((!(localFile.isFile())) || (!(localFile.exists())) || (localFile.lastModified() + 604800000L >= l))
                continue;
            e.a("Utils.RemoteBitmap", new Object[] { "delete  cache : " + localFile });
            localFile.delete();
        }
    }
}